#include "musicdatabase.h"

MusicDatabase::MusicDatabase(QObject *parent,const QString &fileName)\
    : QObject(parent)
{
    thread = new QThread;
    connect(thread,&QThread::started,this,[this,fileName](){
        QSqlDatabase musicDB = QSqlDatabase::addDatabase("QSQLITE","music-database");
        musicDB.setDatabaseName(fileName);
        bool ok = musicDB.open();
        if(!ok){
            qDebug()<<"faile to open"<<fileName<<",err:"<<musicDB.lastError().text();
            return;
        }
        //创建表
        createMusicTable();
        //扫描整张表
        scanAllTable();
    });
    this->moveToThread(thread);
    thread->start();
}

//创建表--localMusicTable
void MusicDatabase::createMusicTable()
{
    QSqlDatabase musicDB = QSqlDatabase::database("music-database");
    QSqlQuery query(musicDB);
    bool ok = query.exec("CREATE TABLE IF NOT EXISTS localMusicTable("
               "musicName TEXT NOT NULL,"
               "musicAbsolutePath TEXT PRIMARY KEY NOT NULL,"
               "lyricName TEXT,"
               "lyricAbsolutePath TEXT)");
    if(!ok){
        qDebug()<<"fail to create table,err:"<<query.lastError().text();
        return ;
    }
    return;
}

//扫描表--一整张表
void MusicDatabase::scanAllTable()
{
    QSqlDatabase musicDB = QSqlDatabase::database("music-database");
    QSqlQuery query(musicDB);
    bool ok = query.exec("SELECT * FROM localMusicTable");
    if(!ok){
        qDebug()<<"Fail to execute query, error,err:"<<query.lastError().text();
        return ;
    }
    //遍历所有结果
    while(query.next()){
        QStringList musicInfoList;
        QString mp3Name=query.value(0).toString();
        QString mp3Path=query.value(1).toString();
        QFile mp3File(mp3Path);
        if(mp3File.exists()){
            QString lrcName=query.value(2).toString();
            QString lrcPath=query.value(3).toString();
            musicInfoList<<mp3Name<<mp3Path<<lrcName<<lrcPath;
            //发送信号，将这一条数据发送出去
            emit resultOfSelect(musicInfoList);
        }
        else{
            deleteDataOnTable(mp3Path);
        }

    }
    return;
}

//插入数据
void MusicDatabase::insertDataIntoTable(QStringList musicInfoList)
{
    //拿到歌曲的绝对路径
    QString path = musicInfoList[1];
    QSqlDatabase musicDB = QSqlDatabase::database("music-database");
    QSqlQuery query(musicDB);
    query.prepare("SELECT * FROM localMusicTable WHERE musicAbsolutePath = :path");
    query.bindValue(":path", path);
    bool ok = query.exec();
    if(!ok){
        qDebug()<<"fail to query data of table,err:"<<query.lastError().text();
        return ;
    }
    //如果数据存在---不执行操作
    if(query.next()){
        return;
    }
    else{
        QSqlDatabase musicDB = QSqlDatabase::database("music-database");
        QSqlQuery query(musicDB);
        query.prepare("INSERT INTO localMusicTable VALUES(?,?,?,?)");
        query.bindValue(0, musicInfoList[0]);
        query.bindValue(1, musicInfoList[1]);
        query.bindValue(2, musicInfoList[2]);
        query.bindValue(3, musicInfoList[3]);
        bool ok = query.exec();
        if(!ok){
            qDebug()<<"fail to insert data,err:"<<query.lastError().text();
            return ;
        }
    }

    return ;
}

//删除数据--只删除数据库
void MusicDatabase::deleteDataOnTable(QString mp3Path)
{
    QSqlDatabase musicDB = QSqlDatabase::database("music-database");
    QSqlQuery query(musicDB);
    query.prepare("DELETE FROM localMusicTable WHERE musicAbsolutePath = :mp3Path");
    query.bindValue(":mp3Path",mp3Path);
    bool ok = query.exec();
    if(!ok){
        qDebug()<<"fail to delete data,err:"<<query.lastError().text();
        return ;
    }
    return ;
}

//删除数据库信息的同时，将本地文件也删除
void MusicDatabase::deleteMusicOnSystem(QString mp3Path)
{
    QFile file(mp3Path);
    if (file.exists()) {
        bool removed = file.remove();
        if (!removed) {
            qDebug() << "Failed to delete file:" << mp3Path;
        } else {
            qDebug() << "File deleted successfully:" << mp3Path;
        }
    } else {
        qDebug() << "File does not exist:" << mp3Path;
    }
}

